package com.eorchis.module.signupdept.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.ui.commond.IQueryCommond;
import com.eorchis.module.department.domain.DepartmentTreeCondition;
import com.eorchis.module.signupdept.dao.ISignupDeptDao;
import com.eorchis.module.signupdept.domain.SignupDept;
import com.eorchis.module.signupdept.ui.commond.SignupDeptQueryCommond;

/**
 * Title: SignupDeptDaoImpl<br>
 * Description: 报名单位数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2014-12-29
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.signupdept.dao.impl.SignupDeptDaoImpl")
public class SignupDeptDaoImpl
					extends HibernateAbstractBaseDao
					implements ISignupDeptDao {
	
	public Class<? extends IBaseEntity> entityClass() {
		return SignupDept.class;
	}
	
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		SignupDeptQueryCommond signupDeptQueryCommond = (SignupDeptQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM SignupDept t");
		defaultQueryConditionBuilder.addCondition("t.id", CompareType.IN, signupDeptQueryCommond.getSearchIds());
		defaultQueryConditionBuilder.addCondition("t.id", CompareType.EQUAL,signupDeptQueryCommond.getSearchId());
		defaultQueryConditionBuilder.addCondition("t.depid", CompareType.EQUAL,signupDeptQueryCommond.getSearchDepId());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
	}
	
	
	/**
	 * 部门树查询
	 * 
	 * @param condition
	 * @return
	 * @author Max
	 * @createDate 2014-12-29
	 */
	@Override
	public List<SignupDept> listDepartmentTree(DepartmentTreeCondition condition) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("parentId", condition.getNode());
		List<SignupDept> list = (List<SignupDept>) this.executeFind(QueryStringType.HQL, this.listDepartmentTreeHQL(condition), values);
		return list;
	}
	
	
	/**
	 * 部门树查询sql
	 * 
	 * @param condition
	 * @return
	 * @author Max
	 * @createDate 2014-12-29
	 */
	public String listDepartmentTreeHQL(DepartmentTreeCondition condition) {
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("select t from SignupDept t where 1=1");
		if (condition.getNode() != null && !"".equals(condition.getNode())) {
			sqlBuffer.append(" and t.parentId= :parentId");
		}
		sqlBuffer.append("  order by t.orderNum");
		sqlBuffer.toString();
		return sqlBuffer.toString();
	}
	
	/**
	 * 查询部门是否有子节点
	 * 
	 * @param condition
	 * @return
	 * @author Max
	 * @createDate 2014-12-30
	 */
	@Override
	public boolean isLeapNode(DepartmentTreeCondition condition) throws Exception {
		StringBuffer hqlBuffer = new StringBuffer();
		hqlBuffer.append("select count(d.depid) from SignupDept d where 1=1");
		Map<String, Object> values = new HashMap<String, Object>();
		if (condition.getNode() != null && !"".equals(condition.getNode())) {
			hqlBuffer.append(" and d.parentId= :parentId");
			values.put("parentId", condition.getNode());
		}
		boolean result = true;
		List<Object> list = (List<Object>) this.executeFind(QueryStringType.HQL, hqlBuffer.toString(), values);
		if (Integer.parseInt(list.get(0).toString())>0) {
			result = false;
		}
		return result;
	}

}
